#ifndef FBXVIEWERWIDGET_H
#define FBXVIEWERWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLTexture>
#include <QTimer>
#include <QDebug>
#include <QMatrix4x4>
#include <QVector3D>
#include <memory>

#include "OpenFBX/ofbx.h"

// 自定义 IScene 删除器
struct ISceneDeleter {
    void operator()(ofbx::IScene* ptr) const {
        if (ptr) {
            ptr->destroy();
        }
    }
};

struct Vertex {
    QVector3D position;
    QVector3D normal;
    QVector2D texCoord;
};

struct TextureData {
    QOpenGLTexture* texture;
    QString filePath;

    TextureData() : texture(nullptr) {}
};

struct MeshData {
    QOpenGLVertexArrayObject VAO;
    QOpenGLBuffer VBO;
    QOpenGLBuffer EBO;
    int indexCount;
    QMatrix4x4 transform;
    TextureData* textureData;

    MeshData() : indexCount(0),textureData(nullptr) {}

    // 删除拷贝和移动操作
    MeshData(const MeshData&) = delete;
    MeshData& operator=(const MeshData&) = delete;
    MeshData(MeshData&&) = delete;
    MeshData& operator=(MeshData&&) = delete;
};

class FBXViewerWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
{
    Q_OBJECT

public:
    explicit FBXViewerWidget(QWidget *parent = nullptr);
    ~FBXViewerWidget();

    bool loadFBXFile(const QString& filePath);
    inline void setDefaultTexturePath(QString path) { m_defaulttexturepath = path; }


protected:
    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;
    void keyPressEvent(QKeyEvent* event) override;
    void wheelEvent(QWheelEvent* event) override; // 添加滚轮事件

    // 添加鼠标事件处理
    void mousePressEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent* event) override;

private slots:
    void updateScene();

private:
    void setupShaders();
    void checkGLError(const char* location);
    void cleanupFBXData();
    void processFBXScene();
    void processMesh(const ofbx::Mesh* mesh, const QMatrix4x4& transform);
    QMatrix4x4 toQMatrix(const ofbx::DMatrix& fbxMatrix);
    QVector3D toQVector(const ofbx::DVec3& vec);
    QVector2D toQVector(const ofbx::Vec2& vec);
    QVector3D toQVector(const ofbx::Vec3& vec);
    TextureData* loadTexture(const ofbx::Texture* fbxTexture);
    TextureData* loadTextureFromFile(void);
    TextureData* createDefaultTexture();

    // 着色器和基本渲染
    QOpenGLShaderProgram* m_shaderProgram;
    QOpenGLVertexArrayObject m_testVAO;
    QOpenGLBuffer m_testVBO;
    QTimer* m_timer;
    int m_vertexCount;

    QVector<TextureData*> m_textures;
    QString m_textureSearchPath,m_defaulttexturepath;

    // FBX 相关
    std::unique_ptr<ofbx::IScene, ISceneDeleter> m_scene;
    QVector<MeshData*> m_fbxMeshes;  // 使用指针存储
    QMatrix4x4 m_viewMatrix;
    QMatrix4x4 m_projectionMatrix;
    float m_rotationAngle;
    float m_cameraDistance;

    float m_rotationX;  // X轴旋转角度
    float m_rotationY;  // Y轴旋转角度
    QPoint m_lastMousePos;
    bool m_isRotating;
};

#endif // FBXVIEWERWIDGET_H
